#include "class_strategy_grid_static.h"
#include "class_center.h"
//#include "class_strategy_position.h"

extern class_center a;
extern lambda_pool pool;


class_strategy_grid_static::class_strategy_grid_static(int idd, std::string dm, double n1, double bj, int sl)
{
memset(&ints,0,sizeof(ints));
memset(&dous,0,sizeof(dous));
memset(&data,0,sizeof(data));

name = dm;

std::string str = a.path_str + "/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/grid_static";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/grid_static/"+dm;
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy/grid_static";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

ints[0] = sl;
dous[0] = n1;
dous[1]=bj;

cc = new class_strategy_position(a.path_str + "/strategy/grid_static/" + dm);

a.tick_event_map[name].insert([this](){ pool.insert([this](){ if(a.flag[50]==1){run_one();} }); },id);
a.log.insert("strategy/grid_static/"+name,"实例化成功,当前仓位" + std::to_string(cc->position));
}

class_strategy_grid_static::~class_strategy_grid_static()
{
a.tick_event_map[name].pop(id);
	a.log.insert("strategy/grid_static/"+name,"析构完成,当前仓位"+std::to_string(cc->position));
delete cc;
}


// 主执行函数
void class_strategy_grid_static::run_one()
{

//获取到TICK
data=a.tick_map[name];

//清理报单返回值
a.return_erase(*cc);
//撤单
a.cd(*cc);
//同步持仓
a.position_sync(*cc);




//下面是数据计算
ints[3]= std::floor((dous[0] - data.AskPrice1)/dous[1] ) ;
ints[4]= std::floor((dous[0] - data.BidPrice1)/dous[1] ) + 1;

ints[3] *= ints[0];
ints[4] *= ints[0];

//下面是报单
//如果处于买入区且仓位不够则
if (cc->position < ints[3] && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '0', data.AskPrice1, 1, *cc);
a.log.insert("strategy/grid_static/" + name +"交易", name + "买入" + std::to_string(data.AskPrice1) );
return;
}


if (cc->position > ints[4] && cc->return_vector.size() == 0)
{
a.bd_auto(data.InstrumentID, '1', data.BidPrice1, 1,*cc);
a.log.insert("strategy/grid_static/" + name + "交易", name + "卖出" + std::to_string(data.BidPrice1) );
return;
}

return;
}

